{extend name="public/container"}
{block name='head_top'}
{/block}
{block name="content"}
<div id="myApp" v-cloak>
    <div class="padding-10">
        <div class="douxue-filter flex flex-between" style="margin-bottom:20px;">
            <div></div>

            <el-button type="primary" size="mini" @click="doAdd()">新增配置</el-button>
        </div>
        <div class="">
            <el-table
                :data="rows"
                style="width: 100%"
                v-loading="loading"
                element-loading-text="加载中"
                element-loading-spinner="el-icon-loading"
                size="mini">
                <el-table-column
                    prop="id"
                    label="ID"
                    width="60"
                    align="left">
                </el-table-column>
                <el-table-column
                    prop="task_name"
                    label="任务名称"
                    align="left">
                </el-table-column>
                <el-table-column
                    prop="golds"
                    width="140"
                    label="奖励金币数"
                    align="left">
                    <template slot-scope="scope">
                        完成奖励{{ scope.row.golds }}个金币
                    </template>
                </el-table-column>
                <el-table-column
                    prop="golds"
                    width="140"
                    label="任务类型"
                    align="left">
                    <template slot-scope="scope">
                        <el-tag type="success" size="mini"  v-if="scope.row.type==1">登录类</el-tag>
                        <el-tag type="warning" size="mini" v-if="scope.row.type==2">邀请浏览类</el-tag>
                        <el-tag type="danger" size="mini" v-if="scope.row.type==3">订单消费类</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    fixed="right"
                    width="140"
                    align="left">
                    <template slot-scope="scope">
                        <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                        <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    small
                    :hide-on-single-page="true"
                    layout="prev, pager, next"
                    :total="rowsCount"
                    :page-size="where.limit"
                    @current-change="doLoadRows">
                </el-pagination>
            </div>
        </div>
    </div>

     <!-- 新增、编辑   -->
    <el-dialog title="新增/编辑" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" size="mini">
            <el-form-item label="任务名称" prop="task_name">
                <el-input v-model="formData.task_name"></el-input>
            </el-form-item>

            <el-form-item label="奖励金币" prop="golds">
                <el-input v-model="formData.golds"></el-input>
            </el-form-item>

            <el-form-item label="任务类型" prop="type">
                <el-select :clearable="true" placeholder="任务类型" v-model="formData.type">
                    <el-option v-for="item in taskTypes" :label="item.val" :key="item.key" :value="item.key"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input v-model="formData.sort"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button size="small" type="primary" @click="doSave('formData')">确 定</el-button>
                <el-button size="small"  @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var _formData = {
        id: 0,
        task_name: '',
        golds: 0,
        type: '',
        sort: 0
    }

    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                where:{
                    page:1,
                    limit:pageSize
                },
                loading:false,
                rowsCount: 0,
                rows: [],
                dialogFormVisible: false,
                formData: _formData,
                rules: {
                    task_name: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ],
                    golds: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ],
                    type: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ]
                },
                taskTypes: []
            }
        },
        methods: {
            doLoadGoodsTypes () {
                axios.get(Url({a:'get_attr' , q:{name:'sign_type'}})).then(res => {
                    this.taskTypes = res.data.data
                })
            }
            ,doLoadRows (page) {
                this.loading = true
                this.where.page  = page || 1
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount =res.data.count
                    this.loading = false
                })
            }
            // 编辑
            ,doAdd ( row ) {
				if ( row != undefined ) {
                    this.formData = row
                } else {
                    this.formData = _formData
                }
                this.dialogFormVisible = true;
            }
            ,doSave (formName ) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData).then(res => {
                            this.$message.success('操作成功')
                            this.dialogFormVisible = false;
                            this.doLoadRows(1)
                        })
                    }
                });
            }
            ,closeDialog(){
                this.$refs['formData'].clearValidate();
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
        },
        mounted(){
            this.doLoadRows()
            this.doLoadGoodsTypes()
        }
    })
</script>
{/block}